<?
require_once('sub.inc.php');
head("Stats");

/* -- User Agents
SELECT count( `message` ) , `message`
FROM `logs`
WHERE `event` = 'HIT'
GROUP BY `message`
ORDER BY `count( ``message`` )` DESC
LIMIT 0 , 999
*/

topTable(1,"hour",3);
topTable(24,"24 Hours",5);
topTable(168,"7 Days",5);
topTable(720,"30 Days",5);


print "<table border=1 width=40% align=center><tr><th colspan=2><font size=+2>Total Link Clicks over...</font></th></tr>\n";
$row = sqlCache("SELECT count(1) as total FROM `logs` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL 1 HOUR)");
print "<tr><td>Last Hour</td><td>{$row['0']['total']}</td></tr>\n";

$row = sqlCache("SELECT count(1) as total FROM `logs` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL 24 HOUR)");
print "<tr><td>Last 24 Hours</td><td>{$row['0']['total']}</td></tr>\n";

$row = sqlCache("SELECT count(1) as total FROM `logs` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL 168 HOUR)");
print "<tr><td>Last 7 Days</td><td>{$row['0']['total']}</td></tr>\n";

$row = sqlCache("SELECT count(1) as total FROM `logs` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL 720 HOUR)");
print "<tr><td>Last 30 Days</td><td>{$row['0']['total']}</td></tr>\n";
print "</table>";

foot();

/*function topTable($time,$display,$limit=10){
	$domlist = array();
	$resd = mysqli_query($GLOBALS['con'], "SELECT * FROM `domains` WHERE 1");
	while($rowd = mysqli_fetch_array($resd)){
		$domlist[$rowd['id']] = $rowd['tld'];
	}

	print "<table border=1 width=100%><tr><th colspan=3><font size=+2>Top $limit most clicked links over the last $display</font></th></tr>\n";
	print "<tr><th width=15%>Click Count</th><th width=15%>Short Code</td><th width=70%>Link</th></tr>\n";
	$SQLoDoom = "SELECT lo.`relatedid` as id,COUNT(lo.`relatedid`) as cnt, li.`code`, li.`url`, li.`domain` FROM `logs` as lo INNER JOIN `links` as li ON lo.`relatedid` = li.`id` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL $time HOUR) GROUP BY `relatedid` ORDER BY cnt desc LIMIT 0 , $limit";
	$res = mysqli_query($GLOBALS['con'], $SQLoDoom);
	while( $row = mysqli_fetch_array($res) ){
		print "<tr><td>{$row['cnt']} </td>";
		print "<td><a href=\"http://{$domlist[$row['domain']]}/{$row['code']}-\">http://{$domlist[$row['domain']]}/{$row['code']}</a></td>";
		print "<td><a href=\"{$row['url']}\">{$row['url']}</a></td></tr>\n";
	}
	print "</table><br>";
}*/

function topTable($time,$display,$limit=10){
	$domlist = array();
	$resd = mysqli_query($GLOBALS['con'], "SELECT * FROM `domains` WHERE 1");
	while($rowd = mysqli_fetch_array($resd)){
		$domlist[$rowd['id']] = $rowd['tld'];
	}

	print "<table border=1 width=100%><tr><th colspan=3><font size=+2>Top $limit most clicked links over the last $display</font></th></tr>\n";
	print "<tr><th width=15%>Click Count</th><th width=15%>Short Code</td><th width=70%>Link</th></tr>\n";
	$SQLoDoom = "SELECT lo.`relatedid` as id,COUNT(lo.`relatedid`) as cnt, li.`code`, li.`url`, li.`domain` FROM `logs` as lo INNER JOIN `links` as li ON lo.`relatedid` = li.`id` WHERE `event` = 'HIT' AND `when`>date_sub(now(), INTERVAL $time HOUR) GROUP BY `relatedid` ORDER BY cnt desc LIMIT 0 , $limit";
	$res = sqlCache($SQLoDoom);
	foreach($res as $row){
		print "<tr><td>{$row['cnt']} </td>";
		print "<td><a href=\"http://{$domlist[$row['domain']]}/{$row['code']}-\">http://{$domlist[$row['domain']]}/{$row['code']}</a></td>";
		print "<td><a href=\"{$row['url']}\">{$row['url']}</a></td></tr>\n";
	}
	print "</table><br>";
}

function sqlCache($sql){

	if($res = memcache_get($GLOBALS['mem'], sha1($sql))){
		if(DEBUG_ON){print "CHIT";}
		return unserialize($res);
	}else{
		if(DEBUG_ON){print "CMISS";}
		$res = mysqli_query($GLOBALS['con'], $sql);
		$one = array();
		while($row = mysqli_fetch_array($res)){
			$one[] = $row;
		}
		memcache_set($GLOBALS['mem'], sha1($sql), serialize($one), 0, 30);
		return $one;
	}
}
